<?php
namespace Tools;
use think\Db;
use app\admin\model\deerhome\SmsLog as SmsLogModel;
use app\admin\model\deerhome\Xcxmsg as XcxmsgModel;
use app\admin\model\deerhome\User as UserModel;
use app\admin\model\deerhome\Worker as WorkerModel;
/**
 * 短信消息类
*/
class SmsMsg
{
    //提醒用户已派单
    public static function toUserPaidan($data){
        $msgCode=XcxmsgModel::where("title","服务派单通知")->where('sms_open',1)->value("sms_template_code");
        if(!$msgCode){
            return ;
        }
        $arr=[];
        $arr["name"]=$data['item_name'];
        $arr["user"]=$data['worker_name'];
        self::sendTo($data['user_phone'],$arr,$msgCode);
    }
    //提醒用户变更时间
    public static function toUserEditTime($data,$newTime){
        $msgCode=XcxmsgModel::where("title","上门时间变更通知")->where('sms_open',1)->value("sms_template_code");
        if(!$msgCode){
            return ;
        }
        $arr=[];
        $arr["name"]=$data['item_name'];
        $arr["day"]=$newTime;
        self::sendTo($data['user_phone'],$arr,$msgCode);
    }
    //分销人员提现审核结果
    public static function toAgentMoneyGet($uid,$status,$price){
        $msgCode=XcxmsgModel::where("title","提现审核通知")->where('sms_open',1)->value("sms_template_code");
        if(!$msgCode){
            return ;
        }
        $userTel=UserModel::where("id",$uid)->value("utel");
        $arr=[];
        $arr["res"]=$status;
        $arr["price"]=$price;
        self::sendTo($userTel,$arr,$msgCode);
        return ;
    }
    //提醒服务人员新的订单
    public static function toWorkerNewOrder($data){
        $msgCode=XcxmsgModel::where("title","新订单通知")->where('sms_open',1)->value("sms_template_code");
        if(!$msgCode){
            return ;
        }
        $arr=[];
        $arr["name"]=$data['item_name'];
        $arr["day"]=$data['date_day']." ".$data['date_time'];
        self::sendTo($data['utel'],$arr,$msgCode);
    }
    //提醒服务人员订单已取消
    public static function toWorkerCancelOrder($data){
        $msgCode=XcxmsgModel::where("title","订单取消通知")->where('sms_open',1)->value("sms_template_code");
        if(!$msgCode){
            return ;
        }
        $userTel=WorkerModel::where("id",$data['worker_id'])->value("utel");
        $arr=[];
        $arr["sn"]=$data['sn'];
        self::sendTo($userTel,$arr,$msgCode);
    }
    //服务人员提现审核结果
    public static function toWorkerMoneyGet($uid,$status,$price,$note){
        $msgCode=XcxmsgModel::where("title","提现审核通知")->where('sms_open',1)->value("sms_template_code");
        if(!$msgCode){
            return ;
        }
        $userTel=WorkerModel::where("id",$uid)->value("utel");
        $arr=[];
        $arr["res"]=$status;
        $arr["price"]=$price;
        self::sendTo($userTel,$arr,$msgCode);
    }
    //服务人员入驻审核结果
    public static function toUserApply($uid,$status,$note){
        $title="审核通过提醒";
        if($status=="未通过"){
            $title="审核未通过提醒";
        }
        $msgCode=XcxmsgModel::where("title",$title)->where('sms_open',1)->value("sms_template_code");
        if(!$msgCode){
            return ;
        }
        //这里是用户申请，还不是服务人员，所以查用户表，而不是服务人员表
        $userTel=UserModel::where("id",$uid)->value("utel");
        $arr=[];
        $arr["name"]="入驻申请";
        self::sendTo($userTel,$arr,$msgCode);
    }
    private static function sendTo($tel,$arr,$tpl){
        try{
            (new SmsLogModel)->send($tel,$arr,$tpl);
        }catch(\Exception $e){
            \think\Log::error("发送短信消息失败："
            .$e->getMessage()
            ."\n {$tpl} {$tel}"
            ."\n".json_encode($arr,JSON_UNESCAPED_UNICODE));
        }
    }
}